Attached are
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Thu, 4 Aug 2005 18:51:55 +0000 (18:51 +0000)
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Thu, 4 Aug 2005 18:51:55 +0000 (18:51 +0000)
three patches to fix a problem with Xend starting consoled.  consoled
depends on xenstored to be running and xenstored is started on demand in
Xend.  The patches change xenstored to manage its own pidfile, and have
xend start actually start up xenstored.

tools/misc/xend
tools/python/xen/xend/server/SrvDaemon.py
tools/xenstore/utils.c

index 706c83842870e84fa164a343e949598b885a0749..734309c99f53ef3c4866af80d3f6ea98aa9db8d2 100644 (file)
@@ -114,6 +114,14 @@ def stop_xcs():
         xcs_pidfile.close()
     except:
        return    
+
+def start_xenstored():
+    if os.fork() == 0:
+        os.execvp('/usr/sbin/xenstored', ['/usr/sbin/xenstored']);
+
+def start_consoled():
+    if os.fork() == 0:
+        os.execvp('/usr/sbin/consoled', ['/usr/sbin/consoled']);
             
 def main():
     try:
@@ -130,11 +138,13 @@ def main():
         return status >> 8
     elif sys.argv[1] == 'start':
         start_xcs()
-        if os.fork() == 0:
-            os.execvp('/usr/sbin/consoled', ['/usr/sbin/consoled']);
+        start_xenstored()
+        start_consoled()
         return daemon.start()
     elif sys.argv[1] == 'trace_start':
         start_xcs()
+        start_xenstored()
+        start_consoled()
         return daemon.start(trace=1)
     elif sys.argv[1] == 'stop':
         stop_xcs()
@@ -142,6 +152,8 @@ def main():
     elif sys.argv[1] == 'restart':
         stop_xcs()
         start_xcs()
+        start_xenstored()
+        start_consoled()
         return daemon.stop() or daemon.start()
     elif sys.argv[1] == 'status':
         return daemon.status()
index c808b19ae1172548f92cdbbcb456ab0db803573a..2b0d6b3b2799823f8d37faa8917f7bc082715a06 100644 (file)
@@ -126,12 +126,8 @@ class Daemon:
     def cleanup_xend(self, kill=False):
         return self.cleanup_process(XEND_PID_FILE, "xend", kill)
 
-    def cleanup_xenstored(self, kill=False):
-        return self.cleanup_process(XENSTORED_PID_FILE, "xenstored", kill)
-
     def cleanup(self, kill=False):
         self.cleanup_xend(kill=kill)
-        #self.cleanup_xenstored(kill=kill)
 
     def status(self):
         """Returns the status of the xend daemon.
@@ -168,31 +164,6 @@ class Daemon:
             pidfile.close()
         return pid
 
-    def start_xenstored(self):
-        """Fork and exec xenstored, writing its pid to XENSTORED_PID_FILE.
-        """
-        def mkdirs(p):
-            try:
-                os.makedirs(p)
-            except:
-                pass
-        mkdirs(XENSTORED_RUN_DIR)
-        mkdirs(XENSTORED_LIB_DIR)
-        
-        pid = self.fork_pid(XENSTORED_PID_FILE)
-        if pid:
-            # Parent
-            log.info("Started xenstored, pid=%d", pid)
-        else:
-            # Child
-            if XEND_DAEMONIZE:
-                self.daemonize()
-            if XENSTORED_DEBUG:
-                os.execl("/usr/sbin/xenstored", "xenstored", "--no-fork",
-                         "-T", "/var/log/xenstored-trace.log")
-            else:
-                os.execl("/usr/sbin/xenstored", "xenstored", "--no-fork")
-
     def daemonize(self):
         if not XEND_DAEMONIZE: return
         # Detach from TTY.
@@ -223,15 +194,11 @@ class Daemon:
         4  Insufficient privileges
         """
         xend_pid = self.cleanup_xend()
-        xenstored_pid = self.cleanup_xenstored()
 
         if self.set_user():
             return 4
         os.chdir("/")
 
-        if xenstored_pid == 0:
-            self.start_xenstored()
-
         if xend_pid > 0:
             # Trying to run an already-running service is a success.
             return 0
index 0b1ac8aedd77802d758744fe17ef8e59bc46cbd4..cc5e0bc5f2d1d510529cc4fc2b47ffd0874991ed 100644 (file)
@@ -84,6 +84,9 @@ void *malloc_nofail(size_t size)
 void daemonize(void)
 {
        pid_t pid;
+       int fd;
+       size_t len;
+       char buf[100];
 
        /* Separate from our parent via fork, so init inherits us. */
        if ((pid = fork()) < 0)
@@ -101,6 +104,18 @@ void daemonize(void)
        chdir("/");
        /* Discard our parent's old-fashioned umask prejudices. */
        umask(0);
+
+       fd = open("/var/run/xenstored.pid", O_RDWR | O_CREAT);
+       if (fd == -1) {
+               exit(1);
+       }
+
+       if (lockf(fd, F_TLOCK, 0) == -1) {
+               exit(1);
+       }
+
+       len = sprintf(buf, "%d\n", getpid());
+       write(fd, buf, len);
 }